#include "DeviceMonitorPanel.h"
#include <QPixmap>
#include <QApplication>
#include <QDir>
#include <QPainter>
#include <QLinearGradient>
#include <QFileInfo>
#include <QDebug>

DeviceMonitorPanel::DeviceMonitorPanel(QWidget *parent)
    : QWidget(parent),
    m_eegIndicator(nullptr),
    m_ecgIndicator(nullptr),
    m_gsrIndicator(nullptr),
    m_logoLabel(nullptr)
{
    setupUI();
}

void DeviceMonitorPanel::setupUI()
{
    // 创建主布局 - 改为垂直布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(0);  // 增加间距
    mainLayout->setContentsMargins(4, 0, 10, 0);  // 增加边距

    // 创建垂直布局，不使用GroupBox
    QVBoxLayout *deviceLayout = new QVBoxLayout();
    deviceLayout->setSpacing(20);  // 增加间距
    deviceLayout->setContentsMargins(0, 0, 0, 0);

    // 创建状态指示器
    m_eegIndicator = new StatusIndicator("EEG", this);
    m_ecgIndicator = new StatusIndicator("ECG", this);
    m_gsrIndicator = new StatusIndicator("GSR", this);

    // 设置粗体字体
    QFont boldFont;
    boldFont.setBold(true);

    // 应用粗体字体到所有设备标签
    m_eegIndicator->setLabelFont(boldFont);
    m_ecgIndicator->setLabelFont(boldFont);
    m_gsrIndicator->setLabelFont(boldFont);

    // 连接状态指示器的点击信号
    connect(m_eegIndicator, &StatusIndicator::clicked, this, [this]() {
        emit deviceStatusClicked("EEG");
    });

    connect(m_ecgIndicator, &StatusIndicator::clicked, this, [this]() {
        emit deviceStatusClicked("ECG");
    });

    connect(m_gsrIndicator, &StatusIndicator::clicked, this, [this]() {
        emit deviceStatusClicked("GSR");
    });

    deviceLayout->addWidget(m_eegIndicator);
    deviceLayout->addWidget(m_ecgIndicator);
    deviceLayout->addWidget(m_gsrIndicator);

    // 添加到主布局
    mainLayout->addLayout(deviceLayout);
    mainLayout->addStretch(1);  // 添加弹性空间，使控件靠上对齐

    setLayout(mainLayout);

    // 调整面板宽度，使其适应垂直布局的指示器
    setMinimumWidth(160);
    // 移除固定高度限制，允许垂直布局自然扩展
    setMinimumHeight(180);

    // 移除面板背景
    setStyleSheet("DeviceMonitorPanel { background-color: transparent; }");

    // 设置属性，使其支持半透明效果
    setAttribute(Qt::WA_TranslucentBackground);
}

void DeviceMonitorPanel::setEEGStatus(StatusIndicator::DeviceStatus status, double quality)
{
    if (m_eegIndicator) {
        m_eegIndicator->setStatus(status);
        m_eegIndicator->setDataQuality(quality);
    }
}

void DeviceMonitorPanel::setECGStatus(StatusIndicator::DeviceStatus status, double quality)
{
    if (m_ecgIndicator) {
        m_ecgIndicator->setStatus(status);
        m_ecgIndicator->setDataQuality(quality);
    }
}

void DeviceMonitorPanel::setGSRStatus(StatusIndicator::DeviceStatus status, double quality)
{
    if (m_gsrIndicator) {
        m_gsrIndicator->setStatus(status);
        m_gsrIndicator->setDataQuality(quality);
    }
}

// 添加设置设备标签字体的方法实现
void DeviceMonitorPanel::setDeviceLabelsFont(const QFont &font)
{
    if (m_eegIndicator) {
        m_eegIndicator->setLabelFont(font);
    }

    if (m_ecgIndicator) {
        m_ecgIndicator->setLabelFont(font);
    }

    if (m_gsrIndicator) {
        m_gsrIndicator->setLabelFont(font);
    }
}


